package com.focusai.efairy.ui.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.focusai.efairy.R;

/**
 * Created by luo_guangwen on 2017/10/10.
 */

public class PopWindowDevChatTypeView extends PopupWindow implements View.OnClickListener {
    private Context mContext;
    private TextView textAll;
    private TextView textFire;
    private TextView textWarn;
    private TextView textError;
    private int type;
    private OnViewItemClickLisenter onViewItemClickLisenter;

    public PopWindowDevChatTypeView(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    private void init() {
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.view_pop_select_filter, null);
        this.setContentView(contentView);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setTouchable(true);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(mContext.getResources().getColor(R.color.transparent));
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popup_window_animation);
        textAll = (TextView) contentView.findViewById(R.id.text_type_all);
        textFire = (TextView) contentView.findViewById(R.id.text_type_fire);
        textWarn = (TextView) contentView.findViewById(R.id.text_type_warning);
        textError = (TextView) contentView.findViewById(R.id.text_type_error);
        textAll.setOnClickListener(this);
        textFire.setOnClickListener(this);
        textWarn.setOnClickListener(this);
        textError.setOnClickListener(this);

    }

    public void show(View view) {
        if (this.isShowing())
            return;
        showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void dismiss() {
        if (this.isShowing()) {
            super.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_type_all:
                dismiss();
                type = -1;
                changeBg();
                break;
            case R.id.text_type_fire:
                dismiss();
                type = 1;
                changeBg();
                break;
            case R.id.text_type_warning:
                dismiss();
                type = 2;
                changeBg();
                break;
            case R.id.text_type_error:
                dismiss();
                type = 3;
                changeBg();
                break;
            default:
                break;
        }
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setOnViewItemClickLisenter(OnViewItemClickLisenter onViewItemClickLisenter) {
        this.onViewItemClickLisenter = onViewItemClickLisenter;
    }

    public void setText() {
        textAll.setText("全部");
        textFire.setText("正常");
        textWarn.setText("故障");
        textError.setText("报警");
    }

    public void changeBg() {
        int color = mContext.getResources().getColor(R.color.white);
        int colorbg = mContext.getResources().getColor(R.color.bg_gray);
        textAll.setBackgroundColor(color);
        textFire.setBackgroundColor(color);
        textWarn.setBackgroundColor(color);
        textError.setBackgroundColor(color);

        switch (type) {
            case -1:
                textAll.setBackgroundColor(colorbg);
                break;
            case 1:
                textFire.setBackgroundColor(colorbg);

                break;
            case 2:
                textWarn.setBackgroundColor(colorbg);

                break;
            case 3:
                textError.setBackgroundColor(colorbg);

                break;
            default:
                break;
        }
        if (onViewItemClickLisenter != null) {
            onViewItemClickLisenter.onViewItemClick(type);
        }
    }

    public static interface OnViewItemClickLisenter {
        void onViewItemClick(int type);
    }
}
